#!/usr/bin/env python
# encoding: utf-8
# @Time    : 2020/8/24 18:40
# @Author  : Mr. Cui
# @File    : views.py
# @Software: PyCharm
# case_library蓝图的视图

import json

from flask import request

from . import case_library
from .service import case_library_service
from ...libs import const
from ...libs.framework import *
from ...libs.validate_params import validate_params


@case_library.route('/add_case', methods=['POST'])
@validate_params(params=["case_name", "group_name", "project_name", "case_value", "case_type"])
def add_case():
    """
    用例仓库-新增
    :return:
    """
    data = json.loads(request.data)
    case_name = data["case_name"]
    group_name = data["group_name"]
    project_name = data["project_name"]
    case_value = data["case_value"]
    case_type = data["case_type"]
    message = case_library_service.add_case(case_name, group_name, project_name, case_type, case_value)
    return result(const.SUCCESS, message, None)


@case_library.route('/update_case', methods=['POST'])
@validate_params(params=["case_name", "group_name", "project_name", "case_value", "case_type", "is_deploy"])
def update_case():
    """
    用例仓库-修改
    :return:
    """
    data = json.loads(request.data)
    message = case_library_service.update_case(data)
    return result(const.SUCCESS, message, None)


@case_library.route('/deployee/<case_id>/<node_id>/<task_id>', methods=["GET"])
def remoteDeplyeee(case_id,node_id,task_id):
    """
    远程控制生成目录以及git代码-部署
    :param case_id: 用例id
    :return:
    """
    ret = case_library_service.call_method(case_id=case_id, sign="deployee", node_id=node_id, task_id=task_id)
    return result(const.SUCCESS, ret, None)


@case_library.route('/stop/<case_id>/<node_id>/<task_id>')
def remoteStop(case_id,node_id,task_id):
    """
    远程控制生成目录以及git代码-停止
    :param case_id: 用例id
    :return:
    """
    ret = case_library_service.call_method(case_id=case_id, sign="stop", node_id=node_id, task_id=task_id)
    return result(const.SUCCESS, ret, None)


@case_library.route('/start/<case_id>/<node_id>/<task_id>')
def remoteStart(case_id,node_id,task_id):
    """
    远程控制生成目录以及git代码-启动
    :param case_id: 用例id
    :return:
    """
    ret = case_library_service.call_method(case_id=case_id, sign="run", node_id=node_id, task_id=task_id)
    return result(const.SUCCESS, ret, None)


@case_library.route('/get_case_list', methods=['post'])
@validate_params(params=["is_deploy", "project_name", "case_type", "case_name", "page_num", "page_per"])
def getCaseList():
    """
    用例仓库列表查询
    :return:
    """
    res = case_library_service.get_case_list(request.json)
    return result(const.SUCCESS, const.QUERY_SUCCESS, res)
